{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "default-image-extension: \"\"\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Test it out yourself in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1xxGlTbwBmaY6GKA24strRixTXGBOlyiw)\n",
    "[![](https://dcbadge.vercel.app/api/server/FANw4rHD5E)](https://discord.gg/FANw4rHD5E)  \n",
    "*If you have questions or you want to help you can find us in the #audio-generation channel on the LAION Discord server.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# WhisperSpeech"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An Open Source text-to-speech system built by inverting Whisper. Previously known as **spear-tts-pytorch**.\n",
    "\n",
    "We want this model to be like Stable Diffusion but for speech – both powerful and easily customizable.\n",
    "\n",
    "We are working only with properly licensed speech recordings and all the code is Open Source so the model will be always safe to use for commercial applications.\n",
    "\n",
    "Currently the models are trained on the English LibreLight dataset. In the next release we want to target multiple languages (Whisper and EnCodec are both multilanguage).\n",
    "\n",
    "Sample of the synthesized voice:\n",
    "\n",
    "https://github.com/collabora/WhisperSpeech/assets/107984/aa5a1e7e-dc94-481f-8863-b022c7fd7434"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Progress update [2024-01-18]\n",
    "\n",
    "We spend the last week optimizing inference performance. We integrated `torch.compile`, added kv-caching and tuned some of the layers – we are now working over 12x faster than real-time on a consumer 4090!\n",
    "\n",
    "We can mix languages in a single sentence (here the highlighted English project names are seamlessly mixed into Polish speech):\n",
    "\n",
    "> To jest pierwszy test wielojęzycznego `Whisper Speech` modelu zamieniającego tekst na mowę, który `Collabora` i `Laion` nauczyli na superkomputerze `Jewels`.\n",
    "\n",
    "https://github.com/collabora/WhisperSpeech/assets/107984/d7092ef1-9df7-40e3-a07e-fdc7a090ae9e\n",
    "\n",
    "We also added an easy way to test voice-cloning. Here is a sample voice cloned from [a famous speech by Winston Churchill](https://en.wikipedia.org/wiki/File:Winston_Churchill_-_Be_Ye_Men_of_Valour.ogg) (the radio static is a feature, not a bug ;) – it is part of the reference recording):\n",
    "\n",
    "https://github.com/collabora/WhisperSpeech/assets/107984/bd28110b-31fb-4d61-83f6-c997f560bc26\n",
    "\n",
    "You can [test all of these on Collab](https://colab.research.google.com/drive/1xxGlTbwBmaY6GKA24strRixTXGBOlyiw) (we optimized the dependencies so now it takes less than 30 seconds to install). A Huggingface Space is coming soon.\n",
    "\n",
    "## Progress update [2024-01-10]\n",
    "\n",
    "We've pushed a new SD S2A model that is a lot faster while still generating high-quality speech. We've also added an example of voice cloning based on a reference audio file.\n",
    "\n",
    "As always, you can [check out our Colab](https://colab.research.google.com/drive/1xxGlTbwBmaY6GKA24strRixTXGBOlyiw) to try it yourself!\n",
    "\n",
    "## Progress update [2023-12-10]\n",
    "\n",
    "Another trio of models, this time they support multiple languages (English and Polish). Here are two new samples for a sneak peek. You can [check out our Colab](https://colab.research.google.com/drive/1xxGlTbwBmaY6GKA24strRixTXGBOlyiw) to try it yourself!\n",
    "\n",
    "English speech, female voice (transferred from a Polish language dataset):\n",
    "\n",
    "https://github.com/collabora/WhisperSpeech/assets/107984/aa5a1e7e-dc94-481f-8863-b022c7fd7434\n",
    "\n",
    "A Polish sample, male voice:\n",
    "\n",
    "https://github.com/collabora/WhisperSpeech/assets/107984/4da14b03-33f9-4e2d-be42-f0fcf1d4a6ec\n",
    "\n",
    "[Older progress updates are archived here](https://github.com/collabora/WhisperSpeech/issues/23)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Downloads\n",
    "\n",
    "We encourage you to start with the Google Colab link above or run the provided notebook locally.\n",
    "If you want to download manually or train the models from scratch then both [the WhisperSpeech pre-trained models](https://huggingface.co/collabora/whisperspeech)\n",
    "as well as [the converted datasets](https://huggingface.co/datasets/collabora/whisperspeech) are available on HuggingFace."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Roadmap\n",
    "\n",
    "- [ ] [Gather a bigger emotive speech dataset](https://github.com/collabora/spear-tts-pytorch/issues/11)\n",
    "- [ ] Figure out a way to condition the generation on emotions and prosody\n",
    "- [ ] Create a community effort to gather freely licensed speech in multiple languages\n",
    "- [ ] [Train final multi-language models](https://github.com/collabora/spear-tts-pytorch/issues/12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Architecture\n",
    "\n",
    "The general architecture is similar to [AudioLM](https://google-research.github.io/seanet/audiolm/examples/), [SPEAR TTS](https://google-research.github.io/seanet/speartts/examples/) from Google and [MusicGen](https://ai.honu.io/papers/musicgen/) from Meta. We avoided the NIH syndrome and built it on top of powerful Open Source models: [Whisper](https://github.com/openai/whisper) from OpenAI to generate semantic tokens and perform transcription, [EnCodec](https://github.com/facebookresearch/encodec) from Meta for acoustic modeling and [Vocos](https://github.com/charactr-platform/vocos) from Charactr Inc as the high-quality vocoder.\n",
    "\n",
    "We gave two presentation diving deeper into WhisperSpeech. The first one talks about the challenges of large scale training:\n",
    "\n",
    "[![Tricks Learned from Scaling WhisperSpeech Models to 80k+ Hours of Speech - video recording by Jakub Cłapa, Collabora](https://img.youtube.com/vi/6Fr-rq-yjXo/0.jpg)](https://www.youtube.com/watch?v=6Fr-rq-yjXo)\n",
    "\n",
    "The other one goes a bit more into the architectural choices we made:\n",
    "\n",
    "[![Open Source Text-To-Speech Projects: WhisperSpeech - In Depth Discussion\n",
    "](https://img.youtube.com/vi/1OBvf33S77Y/0.jpg)](https://www.youtube.com/watch?v=1OBvf33S77Y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Whisper for modeling semantic tokens\n",
    "\n",
    "We utilize the OpenAI Whisper encoder block to generate embeddings which we then quantize to get semantic tokens.\n",
    "\n",
    "If the language is already supported by Whisper then this process requires only audio files (without ground truth transcriptions).\n",
    "\n",
    "![Using Whisper for semantic token extraction diagram](whisper-block.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## EnCodec for modeling acoustic tokens\n",
    "\n",
    "We use EnCodec to model the audio waveform. Out of the box it delivers reasonable quality at 1.5kbps and we can bring this to high-quality by using Vocos – a vocoder pretrained on EnCodec tokens.\n",
    "\n",
    "![EnCodec block diagram](https://github.com/facebookresearch/encodec/raw/main/architecture.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Appreciation\n",
    "\n",
    "[<img height=80 src=\"https://user-images.githubusercontent.com/107984/229537027-a6d7462b-0c9c-4fd4-b69e-58e98c3ee63f.png\" alt=\"Collabora logo\">](https://www.collabora.com)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<img height=80 src=\"https://user-images.githubusercontent.com/107984/229535036-c741d775-4a9b-4193-89a0-9ddb89ecd011.png\" alt=\"LAION logo\">](https://laion.ai)\n",
    "\n",
    "This work would not be possible without the generous sponsorships from:\n",
    "\n",
    "- [Collabora](https://www.collabora.com) – code development and model training\n",
    "- [LAION](https://laion.ai) – community building and datasets (special thanks to \n",
    "- [Jülich Supercomputing Centre](https://www.fz-juelich.de/en) - JUWELS Booster supercomputer\n",
    "\n",
    "We gratefully acknowledge the Gauss Centre for Supercomputing e.V. (www.gauss-centre.eu) for funding part of this work by providing computing time through the John von Neumann Institute for Computing (NIC) on the GCS Supercomputer JUWELS Booster at Jülich Supercomputing Centre (JSC), with access to compute provided via LAION cooperation on foundation models research.\n",
    "\n",
    "We'd like to also thank individual contributors for their great help in building this model:\n",
    "\n",
    "- [inevitable-2031](https://github.com/inevitable-2031) (`qwerty_qwer` on Discord) for dataset curation "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Consulting\n",
    "\n",
    "We are available to help you with both Open Source and proprietary AI projects. You can reach us via the Collabora website or on Discord ([![](https://dcbadge.vercel.app/api/shield/270267134960074762?style=flat)](https://discordapp.com/users/270267134960074762) and [![](https://dcbadge.vercel.app/api/shield/1088938086400016475?style=flat)](https://discordapp.com/users/1088938086400016475))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Citations\n",
    "\n",
    "We rely on many amazing Open Source projects and research papers:\n",
    "\n",
    "```bibtex\n",
    "@article{SpearTTS,\n",
    "  title = {Speak, Read and Prompt: High-Fidelity Text-to-Speech with Minimal Supervision},\n",
    "  url = {https://arxiv.org/abs/2302.03540},\n",
    "  author = {Kharitonov, Eugene and Vincent, Damien and Borsos, Zalán and Marinier, Raphaël and Girgin, Sertan and Pietquin, Olivier and Sharifi, Matt and Tagliasacchi, Marco and Zeghidour, Neil},\n",
    "  publisher = {arXiv},\n",
    "  year = {2023},\n",
    "}\n",
    "```\n",
    "\n",
    "```bibtex\n",
    "@article{MusicGen,\n",
    "  title={Simple and Controllable Music Generation}, \n",
    "  url = {https://arxiv.org/abs/2306.05284},\n",
    "  author={Jade Copet and Felix Kreuk and Itai Gat and Tal Remez and David Kant and Gabriel Synnaeve and Yossi Adi and Alexandre Défossez},\n",
    "  publisher={arXiv},\n",
    "  year={2023},\n",
    "}\n",
    "```\n",
    "\n",
    "```bibtex\n",
    "@article{Whisper\n",
    "  title = {Robust Speech Recognition via Large-Scale Weak Supervision},\n",
    "  url = {https://arxiv.org/abs/2212.04356},\n",
    "  author = {Radford, Alec and Kim, Jong Wook and Xu, Tao and Brockman, Greg and McLeavey, Christine and Sutskever, Ilya},\n",
    "  publisher = {arXiv},\n",
    "  year = {2022},\n",
    "}\n",
    "```\n",
    "\n",
    "```bibtex\n",
    "@article{EnCodec\n",
    "  title = {High Fidelity Neural Audio Compression},\n",
    "  url = {https://arxiv.org/abs/2210.13438},\n",
    "  author = {Défossez, Alexandre and Copet, Jade and Synnaeve, Gabriel and Adi, Yossi},\n",
    "  publisher = {arXiv},\n",
    "  year = {2022},\n",
    "}\n",
    "```\n",
    "\n",
    "```bibtex\n",
    "@article{Vocos\n",
    "  title={Vocos: Closing the gap between time-domain and Fourier-based neural vocoders for high-quality audio synthesis}, \n",
    "  url = {https://arxiv.org/abs/2306.00814},\n",
    "  author={Hubert Siuzdak},\n",
    "  publisher={arXiv},\n",
    "  year={2023},\n",
    "}\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
